home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / hsc / source / riscos / unixname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-02  |  3.5 KB  |  128 lines

  1. /*
  2.  * This source code is part of hsc, a html-preprocessor,
  3.  * Copyright (C) 1997  Sergio Monesi and Nick Craig-Wood
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "unixname.h"
  24.  
  25. extern FILE *gopen(const char *, const char *);
  26. extern FILE *greopen(const char *, const char *, FILE *);
  27. extern int gemove(const char *);
  28. extern int gename(const char *, const char *);
  29.  
  30. /************************************************************
  31. This translates a unix file name to a riscos filename
  32.  
  33. .. -> ^
  34. / <-> .
  35. ************************************************************/
  36.  
  37. char *unixname_to_riscos(const char *unix)
  38. {
  39.     int len = strlen(unix) + 1;
  40.     char *_riscos = malloc(len);
  41.     char *riscos;
  42.     
  43. /* fprintf(stderr, "unix   = '%s'\n", unix); */
  44.     
  45.     if (_riscos == 0)
  46.     {
  47.         fprintf(stderr, "Couldn't allocate memory for file name\n");
  48.         exit(EXIT_FAILURE);
  49.     }
  50.     
  51.     for (riscos = _riscos; *unix; unix++)
  52.     {
  53.         int c = *unix;
  54.         switch (c)
  55.         {
  56.         case '.':
  57.             if (unix[1] == '.')
  58.             {
  59.                 unix++;
  60.                 *riscos++ = '^';
  61.             }
  62.             else
  63.             {
  64.                 *riscos++ = '/';
  65.             }
  66.             break;
  67.         case '/':
  68.             *riscos++ = '.';
  69.             break;
  70.         default:
  71.             *riscos++ = c;
  72.             break;
  73.         }
  74.         
  75.         *riscos = 0;
  76.     }
  77.     
  78. /* fprintf(stderr, "riscos = '%s'\n\n", _riscos); */
  79.  
  80.     return _riscos;
  81. }
  82.  
  83. /************************************************************
  84. ************************************************************/
  85.  
  86. FILE *fopen(const char *unix_filename, const char *mode)
  87. {
  88.     char *riscos_filename = unixname_to_riscos(unix_filename);
  89.     FILE *handle = gopen(riscos_filename, mode);
  90.     free(riscos_filename);
  91.     return handle;
  92. }
  93.  
  94. /************************************************************
  95. ************************************************************/
  96.  
  97. FILE *freopen(const char *unix_filename, const char *mode, FILE *stream)
  98. {
  99.     char *riscos_filename = unixname_to_riscos(unix_filename);
  100.     FILE *handle = greopen(riscos_filename, mode, stream);
  101.     free(riscos_filename);
  102.     return handle;
  103. }
  104.  
  105. /************************************************************
  106. ************************************************************/
  107.  
  108. int remove(const char *unix_filename)
  109. {
  110.     char *riscos_filename = unixname_to_riscos(unix_filename);
  111.     int error = gemove(riscos_filename);
  112.     free(riscos_filename);
  113.     return error;
  114. }
  115.  
  116. /************************************************************
  117. ************************************************************/
  118.  
  119. int rename(const char *unix_old, const char *unix_new)
  120. {
  121.     char *riscos_old = unixname_to_riscos(unix_old);
  122.     char *riscos_new = unixname_to_riscos(unix_new);
  123.     int error = gename(riscos_old, riscos_new);
  124.     free(riscos_old);
  125.     free(riscos_new);
  126.     return error;
  127. }
  128.